<template>
  <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <span>课题申报</span>
      </div>
    </template>
    
    <div class="search-form">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="formInline.name" placeholder="请输入课题名称" clearable @keyup.enter="onSubmit"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="formInline.keyword" placeholder="请输入课题负责人" clearable @keyup.enter="onSubmit"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">
            <el-icon>
              <Search />
            </el-icon>
          </el-button>
        </el-form-item>
      </el-form>
      <div>
        <el-button type="primary" @click="addTopic">新建</el-button>
      </div>
    </div>

    <div class="table-data" v-loading="loading">
      <el-table
        :data="topicData"
        stripe
        borderresizable
        highlight-hover-row
        show-overflow
        keep-source
        scrollbar-always-on
      >
        <el-table-column
          v-for="(tableItem, tableIndex) in topicTableColumns" :key="tableIndex"
          :prop="tableItem.field"
          :label="tableItem.label"
          :min-width="tableItem.minWidth"
          :formatter="tableItem.formatter"
          :show-overflow-tooltip="tableItem.field != 'newsPicture'"
        >
          <template #default="scope" v-if="tableItem.slot">
            <slot :name="tableItem.slot" v-bind="scope">
              <el-image class="table-item-img" :src="reFileResult(scope.row.newsPicture)" fit="cover">
                <template #error>
                  <div class="image-slot">
                    <el-image style="width: 200px;" src="/images/course_default.png" fit="cover" />
                  </div>
                </template>
              </el-image>
            </slot>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          field="opera"
          label="操作"
          min-width="120"
        >
          <template #default="{ row }">
            <el-button
              type="warning"
              link
              @click="topicDetail(row)"
            >查看</el-button>
            <el-button
              type="success"
              link
              @click="topicEdit(row)"
            >编辑</el-button>
            <el-button
              type="success"
              link
              @click="topicDownload(row)"
            >下载</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="table-page">
        <el-pagination background layout="prev, pager, next" :total="dataTotal" :page-size="pageSize" :current-page="currentPage" @current-change="currentChange" />
      </div>
    </div>
  </el-card>
</template>

<script>
export default { name: "backendScienceTopic" }
</script>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router'
import { Search } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { http } from '@/utils/http/index';
import { format } from '@/plugins/moment.js'
import { dateFormatter, checkStatusFormatter, reFileResult } from "@/utils/backend.js"
import { useAuthStore } from '@/stores/modules/auth.js';

/* 路由 */
const route = useRouter();

/* 用户信息 */
const authStore = useAuthStore()
/* 用户权限列表 */
const actionsList = authStore.userInfo.actions

onMounted(() => {
  /* if(!actionsList) return */
  onSubmit()
})

/* 搜索表单 */
const formInline = ref({
  name: "",
  keyword: "",
})

/* 分页 */
const dataTotal = ref(0)
const pageSize = ref(15)
const currentPage = ref(1)

if(route.currentRoute.value.query.currentPage) {
  currentPage.value = Number(route.currentRoute.value.query.currentPage)
}

/* 切换页码方法 */
const currentChange = (val) => {
  currentPage.value = val
  onSubmit()
}

/* 表格数据加载动画 */
const loading = ref(false)

/* 表格数据 */
const topicData = ref([])

/* 表格字段 */
const topicTableColumns = reactive([
  { field: "name", label: "课题名称", minWidth: "200" },
  { field: "keyword", label: "申报日期", minWidth: "200", formatter: dateFormatter },
  { field: "newsPicture", label: "申请单位", minWidth: "250" },
  { field: "newsType", label: "课题负责人", minWidth: "120" },
  { field: "author", label: "成果形式", minWidth: "120" },
  { field: "content", label: "申报内容", minWidth: "120" },
])

/* 列表筛选 */
const onSubmit = () => {
  /* loading.value = true */
  let params = {
    pageIndex: currentPage.value,
    pageSize: pageSize.value,
  }
  let dfi = {
    Logic: 0,
    Filters: []
  }

  for (let k in formInline.value){
    if(formInline.value[k]){
      dfi.Filters.push({
        Field: k,
        Operator: 0,
        Value: formInline.value[k]
      })
    }
  }

  if(dfi.Filters.length){
    params.dfi = dfi
  }
  /* http.$POST("/SocialScienceM/GetSocialScienceNewsInfoList", params).then(res => {
    loading.value = false
    if (res.isError) return
    topicData.value = res.result.dataLst
    dataTotal.value = res.result.countAll
  }).catch(err => {
    loading.value = false
    ElMessage.warning("查询失败，请重试")
  }) */
}

/* 点击新建 */
const addTopic = () => {
  route.push({
    path: "/backend/science-topic/add",
    query: { type: 'add' }
  })
}

/* 点击编辑 */
const topicEdit = (row) => {
  route.push({
    path: "/backend/science-topic/add",
    query: { id: row.id, type: 'edit', currentPage: currentPage.value }
  })
}

/* 点击发布 */
const topicDownload = (row) => {
  console.log("下载")
}

/* 点击查看详情 */
const topicDetail = (row) => {
  let { href } = route.resolve({
    path: "/backend/science-topic/detail",
    query: { id: row.id, currentPage: currentPage.value }
  })
  window.open(href, "_blank")
}
</script>

<style lang='scss' scoped>
@import "@/assets/styles/backendElement.scss";
</style>